<?php defined('SYSPATH') or die('No direct script access.');

 /* $Id: class_group.php 55 2009-12-12 00:58:30Z Colonial Secretary @UndGerman$ */
 
class Messages_Core
{
 function user_message_total($direction=0, $unread_only=FALSE, $where=NULL, $do_joins=FALSE)
  {
    global $database;
    
    $message_total = 0;
    
	  // MAKE SURE MESSAGES ARE ALLOWED
	  if( empty($this->level_info['level_message_allow']) )
      return FALSE;
    
    // BEGIN MESSAGE QUERY
    $sql = "
      SELECT
        COUNT(pmconvoop_id) as pm_total
      FROM
        se_pmconvoops
    ";
    
    // JOIN TO PM AND PMCONVO TABLES
    if( $do_joins ) $sql .= "
      LEFT JOIN
        se_pmconvos
        ON se_pmconvos.pmconvo_id=se_pmconvoops.pmconvoop_pmconvo_id
      LEFT JOIN
        se_pms
        ON se_pms.pm_pmconvo_id=se_pmconvoops.pmconvoop_pmconvo_id
    ";
    
    $sql .= "
      WHERE
        se_pmconvoops.pmconvoop_user_id='{$this->user_info['user_id']}'
    ";
    
    // INCOMING MESSAGES
    if( !$direction ) $sql .= " &&
        se_pmconvoops.pmconvoop_deleted_inbox=0
    ";
    
    // OUTGOING MESSAGES
    if(  $direction ) $sql .= " &&
        /*
        THIS IS REMOVED BECAUSE I AM HOPING THE deleted_outbox WILL HANDLE IT
        se_pms.pm_authoruser_id='{$this->user_info['user_id']}' &&
        */
        se_pmconvoops.pmconvoop_deleted_outbox=0
    ";
    
    // READ ONLY
    if( $unread_only ) $sql .= " &&
        se_pmconvoops.pmconvoop_read=0
    ";
    
    // ADD WHERE
    if( $where ) $sql .= " &&
        {$where}
    ";
    
    // ADD GROUP BY IF JOINING
    if( $do_joins ) $sql .= "
      GROUP BY
        se_pmconvoops.pmconvoop_pmconvo_id
    ";
    
    // RUN QUERY AND RETURN
    $resource = $database->database_query($sql);
    $result = $database->database_fetch_assoc($resource);
    
    //return (int) $database->database_num_rows($resource);
    return (int) $result['pm_total'];
  }
  
  // END user_message_total() METHOD







  
  //
	// THIS METHOD RETURNS AN ARRAY OF USER'S MESSAGES
  //
	// INPUT:
  //    $start      REPRESENTING THE MESSAGE TO START WITH
	//	  $limit      REPRESENTING THE NUMBER OF MESSAGES TO RETURN
	//	  $direction  (OPTIONAL) REPRESENTING A "0" FOR MESSAGES SENT TO USER AND "1" FOR MESSAGES SENT BY USER
	//	  $where      (OPTIONAL)
  //
	// OUTPUT:
  //    AN ARRAY OF THE USER'S MESSAGES
  //
  
  function &user_message_list($start=NULL, $limit=NULL, $direction=0, $where=NULL)
  {
    global $database;
    
	  $message_array = array();
    
	  // MAKE SURE MESSAGES ARE ALLOWED
	  if( empty($this->level_info['level_message_allow']) )
      return FALSE;
    
    // BEGIN MESSAGE QUERY
    $sql = "
      SELECT
        se_pmconvos.*,
        se_pms.*,
        se_pmconvoops_user.pmconvoop_read, 
				se_users.user_id,
				se_users.user_username,
				se_users.user_fname,
				se_users.user_lname,
				se_users.user_photo
    ";
    
    // GET MESSAGE AUTHOR, REPLIED STATUS
    if( !$direction ) $sql .= ",
				(SELECT TRUE FROM se_pms WHERE pm_pmconvo_id=se_pmconvos.pmconvo_id && pm_authoruser_id='{$this->user_info['user_id']}' ORDER BY pm_id DESC LIMIT 1)
				AS pm_replied
    ";
    
    // CONTINUE QUERY
    $sql .= "
      FROM
        se_pmconvoops AS se_pmconvoops_user
      LEFT JOIN
        se_pmconvos
        ON se_pmconvoops_user.pmconvoop_pmconvo_id=se_pmconvos.pmconvo_id
      LEFT JOIN
        se_pms
        ON se_pms.pm_pmconvo_id=se_pmconvos.pmconvo_id
    ";
    
    // INCOMING MESSAGES - JOIN TO USER TABLE TO GET AUTHOR
    if( !$direction ) $sql .= "
      LEFT JOIN
        se_users
        ON se_users.user_id=se_pms.pm_authoruser_id";
    
    // OUTGOING MESSAGES - JOIN TO PMCONVOOPS AND USER TABLE TO GET RECIPIENT
    if(  $direction ) $sql .= "
      LEFT JOIN
        se_pmconvoops AS se_pmconvoops_other
        ON (se_pmconvoops_other.pmconvoop_pmconvo_id=se_pmconvos.pmconvo_id && se_pmconvoops_other.pmconvoop_user_id!='{$this->user_info['user_id']}')
      LEFT JOIN
        se_users
        ON se_users.user_id=se_pmconvoops_other.pmconvoop_user_id
    ";
    
    
    // CONTINUE QUERY
    $sql .= "
      WHERE
        se_pmconvoops_user.pmconvoop_user_id='{$this->user_info['user_id']}'
    ";
    
    // INCOMING MESSAGES
    if( !$direction ) $sql .= " &&
        se_pmconvoops_user.pmconvoop_deleted_inbox=0
    ";
    
    // OUTGOING MESSAGES
    if(  $direction ) $sql .= " &&
        se_pmconvoops_user.pmconvoop_deleted_outbox=0
    ";
    
    // CONTINUE QUERY
    $sql .= " &&
        se_pms.pm_id=(
          SELECT
            MAX(pm_id)
          FROM
            se_pms
          WHERE
            pm_pmconvo_id=se_pmconvos.pmconvo_id
    ";
    
    // INCOMING MESSAGES
    if( !$direction ) $sql .= " &&
            se_pms.pm_authoruser_id!='{$this->user_info['user_id']}'
    ";
    
    // OUTGOING MESSAGES
    if(  $direction ) $sql .= " &&
            se_pms.pm_authoruser_id='{$this->user_info['user_id']}'
    ";
    
    // CONTINUE QUERY
    $sql .= "
        )
    ";
    
    // ADD WHERE
    if( $where ) $sql .= " && {$where}";
    
    /*
      GROUP BY
        se_pmconvoops_user.pmconvoop_pmconvo_id
    */
    
    $sql .= "
      ORDER BY
        se_pmconvoops_user.pmconvoop_pmdate DESC
        /* se_pms.pm_date DESC */
      LIMIT
        $start, $limit
    ";
    
    // EXECUTE QUERY
    $resource = $database->database_query($sql);
    
    // GET MESSAGES
	  while( $message_info=$database->database_fetch_assoc($resource) )
    {
      // CREATE AN OBJECT FOR MESSAGE AUTHOR/RECIPIENT
      $pm_user = new SEUser();
      $pm_user->user_info['user_id']        = $message_info['user_id'];
      $pm_user->user_info['user_username']  = $message_info['user_username'];
      $pm_user->user_info['user_photo']     = $message_info['user_photo'];
      $pm_user->user_info['user_fname']     = $message_info['user_fname'];
      $pm_user->user_info['user_lname']     = $message_info['user_lname'];
      $pm_user->user_displayname();
      
      // Remove breaks for preview
      $message_info['pm_body'] = str_replace("<br>", "", $message_info['pm_body']);
      
      // SET MESSAGE ARRAY
      $message_array[] = array(
        'pmconvo_id'      => $message_info['pmconvo_id'],
        'pmconvo_subject' => $message_info['pmconvo_subject'],
        'pm_date'         => $message_info['pm_date'],
        'pm_read'         => (bool) $message_info['pmconvoop_read'],
        'pm_replied'      => $message_info['pm_replied'],
        'pm_body'         => $message_info['pm_body'],
        'pm_user'         => &$pm_user,
        'pm_recipients'   => $message_info['pmconvo_recipients'] - 1
      );
      
      unset($pm_user);
    }
    
    return $message_array;
  }
  
  // END user_message_list() METHOD








  //
	// THIS METHOD SENDS A MESSAGE TO ANOTHER USER
  //
	// INPUT:
  //    $to REPRESENTING A SEMI-COLON DELIMITED STRING OF USERNAMES OF THE RECIPIENTS
	//	  $subject REPRESENTING THE SUBJECT OF THE MESSAGE
	//	  $message REPRESENTING THE MESSAGE BODY
	//	  $convo_id (OPTIONAL) REPRESENTING THE CONVERSATION ID
  //
	// OUTPUT: 
  //    void
  //
  
  function user_message_send($to, $subject, $message, $convo_id=NULL)
  {
    global $database, $notify, $url;
    
    $recipients = array();
    $recipients_full = array();
    
	  // VALIDATE CONVERSATION ID
	  if( !$convo_id || !is_numeric($convo_id) )
      $convo_id = 0;
    
	  // CHECK TO SEE IF MESSAGE IS EMPTY
	  if( !trim($message) )
      $this->is_error = 796;
    
	  // NEW MESSAGE
	  if( !$convo_id )
    {
	    // ORGANIZE RECIPIENTS
	    $tos = array_filter(preg_split('/[\s,;]+?/', $to));
	    array_splice($tos, $this->level_info['level_message_recipients']);
      
	    // LOOP OVER RECIPIENTS
      foreach( $tos as $to_username )
      {
        // CANT SEND TO SELF
        if( strtolower($to_username)==strtolower($this->user_info['user_username']) ) continue;
        
        // GET TO USER OBJECT
	      $to_user = new SEUser(array(NULL, $to_username));
        
        // CANT SEND TO NON EXISTENT USER. BLOCKED USER, OR USERS NOT ALLOWED TO USE MESSAGES
        if( !$to_user->user_exists ) continue;
        if( $to_user->user_blocked($this->user_info['user_id']) ) continue;
        if( !$this->level_info['level_message_allow'] ) continue;
        
        // CHECK MESSAGE TYPES AND ADD RECIPIENT
        if( $this->level_info['level_message_allow']==2 || ($this->level_info['level_message_allow']==1 && $this->user_friended($to_user->user_info['user_id'])) )
        {
          $recipients_full[$to_user->user_info['user_id']] =& $to_user;
          $recipients[] = $to_user->user_info['user_id'];
        }
	    }
      
      
	    // ENSURE THERE ARE RECIPIENTS
	    if( empty($recipients) )
        $this->is_error = 795;
      
      
	    // IF NO ERROR, CREATE CONVERSATION
	    if( !$this->is_error )
      {
        // CREATE CONVO
        $sql = "INSERT INTO se_pmconvos (pmconvo_subject, pmconvo_recipients) VALUES ('".addslashes($subject)."', '".(count($recipients)+1)."')";
        $resource = $database->database_query($sql);
	      $convo_id = $database->database_insert_id();
        
        // CREATE CONVOOPS
        $sql = "
          INSERT INTO se_pmconvoops
            (pmconvoop_pmconvo_id, pmconvoop_user_id, pmconvoop_deleted_outbox, pmconvoop_deleted_inbox)
          VALUES
            ('{$convo_id}', '{$this->user_info['user_id']}', 0, 1)";
        
        //$is_first = TRUE;
        foreach( $recipients as $to_user_id )
          $sql .= ", ('{$convo_id}', '{$to_user_id}', 1, 0)";
        
        // EXECUTE QUERY
        $resource = $database->database_query($sql);
	    }
    }
    
    // GET RECIPIENTS AND VERIFY USER IS PART OF CONVERSATION
    else
    {
      $sql = "SELECT pmconvoop_user_id FROM se_pmconvoops WHERE pmconvoop_pmconvo_id='{$convo_id}'";
      $resource = $database->database_query($sql);
      
      $unauthorized = TRUE;
      while( $pmconvoop_info=$database->database_fetch_assoc($resource) )
      {
        if( $pmconvoop_info['pmconvoop_user_id']!=$this->user_info['user_id'] )
          $recipients[] = $pmconvoop_info['pmconvoop_user_id'];
        else
          $unauthorized = FALSE;
      }
      
      // USER WAS NOT IN CONVERSATION
      if( $unauthorized )
        $this->is_error = 39; // FIX THIS CODE RANDOM NUMBER TEMP
    }
    
	  // IF NO ERROR, ADD MESSAGE TO CONVERSATION
	  if( !$this->is_error )
    {
	    // LINK ALL LINKS
	    $message = ereg_replace("http://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $message);
	    $message = ereg_replace("(^| |\n)(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $message);
      
	    // RUN SECURITY ON THE MESSAGE TO ENSURE NO XSS ATTACKS WITH LINKS
	    $message = cleanHTML($message, "a");
      
	    // REPLACE NEWLINES IN BODY WITH BREAKS
	    $message = str_replace("\n", "<br>", $message);
	    $message = str_replace("'", "\'", $message);
      
	    // INSERT MESSAGE
	    $pm_date = time();
      
      $sql = "
        INSERT INTO se_pms
          (pm_authoruser_id, pm_pmconvo_id, pm_date, pm_body)
        VALUES
          ('{$this->user_info['user_id']}', '{$convo_id}', '{$pm_date}', '{$message}')
      ";
      
      $resource = $database->database_query($sql);
      
      
      // UPDATE PMCONVOOPS
      $sql = "UPDATE se_pmconvoops SET pmconvoop_deleted_outbox=0, pmconvoop_pmdate='{$pm_date}' WHERE pmconvoop_pmconvo_id='{$convo_id}' && pmconvoop_user_id='{$this->user_info['user_id']}'";
      $resource = $database->database_query($sql);
      
      $sql = "UPDATE se_pmconvoops SET pmconvoop_deleted_inbox=0, pmconvoop_read=0, pmconvoop_pmdate='{$pm_date}' WHERE pmconvoop_pmconvo_id='{$convo_id}' && pmconvoop_user_id!='{$this->user_info['user_id']}'";
      $resource = $database->database_query($sql);
      
      
	    // INSERT/SEND NOTIFICATIONS FOR RECIPIENTS
      // GET RECIPIENTS IF NOT INITIAL MESSAGE
      foreach( $recipients as $recipient_user_id )
      {
        if( empty($recipients_full[$recipient_user_id]) )
        {
          $recipients_full[$recipient_user_id] = new SEUser(array($recipient_user_id));
        }
        
        $current_recipient =& $recipients_full[$recipient_user_id];
        
        // NOT A USER
        if( !is_object($current_recipient) || !$current_recipient->user_exists )
          continue;
        
        // ADD NOTIFICATION
        $notify->notify_add($current_recipient->user_info['user_id'], 'message', $convo_id, array(), array(), TRUE);
        
        // SEND EMAIL
        $current_recipient->user_settings('usersetting_notify_message');
        if( $current_recipient->usersetting_info['usersetting_notify_message'] )
        {
          send_systememail('message', $current_recipient->user_info[user_email], array(
            $current_recipient->user_displayname,
            $this->user_displayname,
            "<a href=\"{$url->url_base}login.php\">{$url->url_base}login.php</a>"
          ));
        }
        
        // CLEAN OUT THEM OLD MESSAGES
        $num_inbox = $current_recipient->user_message_total(0, 0);
        $num_outbox = $current_recipient->user_message_total(1, 0);
        $num_inbox_delete = $num_inbox - $current_recipient->level_info['level_message_inbox'];
        $num_outbox_delete = $num_outbox - $current_recipient->level_info['level_message_outbox'];
        
        // CLEAN OUT INBOX
	      if( $num_inbox_delete>0 )
        {
          $sql = "
            SELECT
              se_pmconvoops.pmconvoop_pmconvo_id AS pmconvo_id
            FROM
              se_pmconvoops
            LEFT JOIN
              se_pmconvos
              ON se_pmconvos.pmconvo_id=se_pmconvoops.pmconvoop_pmconvo_id
            LEFT JOIN
              se_pms
              ON se_pms.pm_pmconvo_id=se_pmconvos.pmconvo_id
            WHERE
              se_pmconvoops.pmconvoop_user_id='{$current_recipient->user_info['user_id']}' &&
              se_pmconvoops.pmconvoop_deleted_inbox=0 &&
              se_pms.pm_id=(SELECT MAX(pm_id) FROM se_pms WHERE pm_pmconvo_id=se_pmconvoops.pmconvoop_pmconvo_id)
            ORDER BY
              se_pms.pm_date ASC
            LIMIT
              {$num_inbox_delete}
          ";
          
          $resource = $database->database_query($sql);
          
          while( $result=$database->database_fetch_assoc($resource) )
            $delete_array[] = $result['pmconvo_id'];
          
          // DELETE
	        $current_recipient->user_message_delete_selected($delete_array, 0);
        }
        
        // CLEAN OUT OUTBOX
        if( $num_outbox_delete>0 )
        {
          $sql = "
            SELECT
              se_pmconvoops.pmconvoop_pmconvo_id AS pmconvo_id
            FROM
              se_pmconvoops
            LEFT JOIN
              se_pmconvos
              ON se_pmconvos.pmconvo_id=se_pmconvoops.pmconvoop_pmconvo_id
            LEFT JOIN
              se_pms
              ON se_pms.pm_pmconvo_id=se_pmconvos.pmconvo_id
            WHERE
              se_pmconvoops.pmconvoop_user_id='{$current_recipient->user_info['user_id']}' &&
              se_pmconvoops.pmconvoop_deleted_outbox=0 &&
              se_pms.pm_id=(SELECT MAX(pm_id) FROM se_pms WHERE pm_pmconvo_id=se_pmconvoops.pmconvoop_pmconvo_id)
            ORDER BY
              se_pms.pm_date ASC
            LIMIT
              {$num_outbox_delete}
          ";
          
          $resource = $database->database_query($sql);
          
          while( $result=$database->database_fetch_assoc($resource) )
            $delete_array[] = $result['pmconvo_id'];
          
          // DELETE
	        $current_recipient->user_message_delete_selected($delete_array, 1);
        }
        
        // CLEAR INACTIVE CONVERSATIONS
        $this->user_message_cleanup();
      }
    }
    
    return $convo_id;
  }
  
  // END user_message_send() METHOD







  
  //
	// THIS METHOD DELETES MANY MESSAGES BASED ON WHAT HAS BEEN POSTED
  //
	// INPUT:
  //    $delete_array CONTAINING THE ARRAY OF CONVERSATION IDs TO DELETE
	//	  $direction (OPTIONAL) REPRESENTING A "0" FOR MESSAGES SENT TO USER AND "1" FOR MESSAGES SENT BY USER
  //
	// OUTPUT: 
  //    void
  //
  
  function user_message_delete_selected($delete_array, $direction=0)
  {
    global $database;
    
	  // START CONSTRUCTING QUERY
    $sql = "
      UPDATE
        se_pmconvoops
      SET
    ";
    
    // INCOMING MESSAGES
    if( !$direction ) $sql .= "
        se_pmconvoops.pmconvoop_deleted_inbox=1
    ";
    
    // OUTGOING MESSAGES
    if(  $direction ) $sql .= "
        se_pmconvoops.pmconvoop_deleted_outbox=1
    ";
    
	  // CONTINUE QUERY
	  $sql .= "
      WHERE
        se_pmconvoops.pmconvoop_user_id='{$this->user_info['user_id']}' &&
        se_pmconvoops.pmconvoop_pmconvo_id IN('".implode("', '", $delete_array)."')
    ";
    
    $database->database_query($sql);
    
	  // DELETE ANY NOTIFICATIONS ASSOCIATED WITH THESE PMs
    $sql = "
      DELETE FROM
        se_notifys
      WHERE
        notify_user_id='{$this->user_info[user_id]}' &&
        notify_notifytype_id='2' &&
        notify_object_id IN('".implode("', '", $delete_array)."')
    ";
    
    $database->database_query($sql);
  }
  
  // END user_message_delete_selected() METHOD







  
  //
	// THIS METHOD CLEANS UP THE PM TABLES
  //
	// INPUT:
  //    void
  //
	// OUTPUT: 
  //    void
  //
  
  function user_message_cleanup()
  {
    global $database;
    
    // CONSTRUCT QUERY
    $sql = "
      SELECT
        SUM(se_pmconvoops.pmconvoop_deleted_inbox) AS total_deleted_inbox,
        SUM(se_pmconvoops.pmconvoop_deleted_inbox) AS total_deleted_outbox,
        se_pmconvos.pmconvo_recipients,
        se_pmconvos.pmconvo_id
      FROM
        se_pmconvos
      LEFT JOIN
        se_pmconvoops
        ON se_pmconvoops.pmconvoop_pmconvo_id=se_pmconvos.pmconvo_id
      GROUP BY
        se_pmconvos.pmconvo_id
      LIMIT
        50
    ";
    
    $resource = $database->database_query($sql);
    
    $to_delete = array();
    while( $result=$database->database_fetch_assoc($resource) && count($to_delete)<50 )
    {
      if( $result['total_deleted_inbox']!=$result['pmconvo_recipients'] ) continue;
      if( $result['total_deleted_outbox']!=$result['pmconvo_recipients'] ) continue;
      $to_delete[] = $result['pmconvo_id'];
    }
    
    $to_delete = array_filter($to_delete);
    
    if( empty($to_delete) )
      return;
    
    $sql = "
      DELETE FROM
        se_pmconvos,
        se_pms,
        se_pmconvoops
      USING
        se_pmconvos
      LEFT JOIN
        se_pms
        ON se_pms.pm_pmconvo_id=se_pmconvos.pmconvo_id
      LEFT JOIN
        se_pmconvoops
        ON se_pmconvoops.pmconvoop_pmconvo_id=se_pmconvos.pmconvo_id
      WHERE
        se_pmconvos.pmconvo_id IN('".join("','", $to_delete)."')
    ";
    
    $resource = $database->database_query($sql);
  }
  
  // END user_message_cleanup() METHOD







  
  //
	// THIS METHOD GETS CONVO INFO IF USER IS PART OF CONVO
  //
	// INPUT:
  //    $convo_id
  //    $validate_only
  //
	// OUTPUT: 
  //    void
  //
  
  function user_message_validate($convo_id, $validate_only=FALSE)
  {
    global $database;
    
    // GET PMCONVO INFO
    $sql = "
      SELECT
        se_pmconvos.*,
        se_pmconvoops.*
      FROM
        se_pmconvos
      LEFT JOIN
        se_pmconvoops
        ON se_pmconvoops.pmconvoop_pmconvo_id=se_pmconvos.pmconvo_id
      WHERE
        se_pmconvos.pmconvo_id='{$convo_id}' &&
        se_pmconvoops.pmconvoop_user_id='{$this->user_info['user_id']}'
      LIMIT
        1
    ";
    
    $resource = $database->database_query($sql);
    
    if( !$database->database_num_rows($resource) )
      return FALSE;
    
    if( $validate_only )
      return TRUE;
    
    return $database->database_fetch_assoc($resource);
  }
  
  // END user_message_validate() METHOD







  
  //
	// THIS METHOD GETS CONVO INFO
  //
	// INPUT:
  //    $convo_id
  //
	// OUTPUT: 
  //    void
  //
  
  function &user_message_view($convo_id)
  {
    global $database;
    
    if( !$this->user_message_validate($convo_id, TRUE) )
      return FALSE;
    
    // SET MESSAGE TO READ
    $sql = "UPDATE se_pmconvoops SET pmconvoop_read=1 WHERE pmconvoop_pmconvo_id='{$convo_id}' && pmconvoop_user_id='{$this->user_info['user_id']}' LIMIT 1";
    $resource = $database->database_query($sql);
    
    // DELETE NOTIFICATIONS
    $sql = "DELETE FROM se_notifys WHERE notify_user_id='{$this->user_info['user_id']}' AND notify_notifytype_id='2' AND notify_object_id='{$convo_id}'";
    $resource = $database->database_query($sql);
    
    // GET COLLABORATORS
    $sql = "
      SELECT
        user_id,
        user_username,
        user_fname,
        user_lname,
        user_photo
      FROM
        se_pmconvoops
      LEFT JOIN
        se_users
        ON se_users.user_id=se_pmconvoops.pmconvoop_user_id
      WHERE
        se_pmconvoops.pmconvoop_pmconvo_id='{$convo_id}' &&
        se_pmconvoops.pmconvoop_user_id!='{$this->user_info['user_id']}'
    ";
    
    $resource = $database->database_query($sql);
    
    $collaborators = array();
    $collaborators_by_id = array();
    while( $result=$database->database_fetch_assoc($resource) )
    {
      $coll = new SEUser();
      $coll->user_info['user_id']       = $result['user_id'];
      $coll->user_info['user_username'] = $result['user_username'];
      $coll->user_info['user_photo']    = $result['user_photo'];
      $coll->user_info['user_fname']    = $result['user_fname'];
      $coll->user_info['user_lname']    = $result['user_lname'];
      $coll->user_displayname();
      
      $collaborators[] =& $coll;
      $collaborators_by_id[$result['user_id']] =& $coll;
      unset($coll);
    }
    
    // GET CONVERSATION
    $sql = "
      SELECT
        se_pms.*
      FROM
        se_pms
      WHERE
        pm_pmconvo_id='{$convo_id}'
      ORDER BY
        pm_date
    ";
    
    $resource = $database->database_query($sql);
    
    $pms = array();
    while( $result=$database->database_fetch_assoc($resource) )
    {
      $pm_info = $result;
      
      if( $pm_info['pm_authoruser_id']==$this->user_info['user_id'] )
        $pm_info['author'] =& $this;
      else
        $pm_info['author'] =& $collaborators_by_id[$pm_info['pm_authoruser_id']];
      
      $pms[] =& $pm_info;
      unset($pm_info);
    }
    
    return array
    (
      'collaborators' => &$collaborators,
      'pms'           => &$pms
    );
  }
  
  // END user_message_view() METHOD

}

?>